//
//  ManSetVC.swift
//  ManGuangDB
//
//  Created by 李东 on 2024/3/26.
//

import UIKit
import SnapKit
import SafariServices

class ManSetVC: ManBaseVC {
    
    lazy var contentView = UIView()
    lazy var serviceView = createItemView(title: NSLocalizedString("服务协议", comment: ""))
    lazy var privacyView = createItemView(title: NSLocalizedString("隐私政策", comment: ""))
    lazy var lineView = UIView()

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        contentView.backgroundColor = .white.withAlphaComponent(0.2)
        contentView.layer.cornerRadius = 15
        contentView.layer.masksToBounds = true
        view.addSubview(contentView)
        contentView.snp.makeConstraints { make in
            make.left.equalTo(15)
            make.right.equalTo(-15)
            make.top.equalTo(50)
            make.height.equalTo(130)
        }
        
        contentView.addSubview(serviceView)
        serviceView.snp.makeConstraints { make in
            make.left.top.right.equalToSuperview()
            make.height.equalTo(65)
        }
        
        contentView.addSubview(privacyView)
        privacyView.snp.makeConstraints { make in
            make.left.right.bottom.equalToSuperview()
            make.top.equalTo(self.serviceView.snp.bottom)
        }
        
        lineView.backgroundColor = UIColor.white.withAlphaComponent(0.5)
        contentView.addSubview(lineView)
        lineView.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.left.equalTo(18)
            make.right.equalTo(-18)
            make.height.equalTo(0.5)
        }

        serviceView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(itemViewClick(sender:))))
        privacyView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(itemViewClick(sender:))))
    }
    
    private func createItemView(title: String) -> UIView {
        let temp = UIView()
                
        let label = UILabel()
        label.textColor = .white
        label.font = UIFont.Medium(ofSize: 16)
        label.text = title
        temp.addSubview(label)
        label.snp.makeConstraints { make in
            make.left.equalTo(18)
            make.centerY.equalToSuperview()
        }
        
        let imgView = UIImageView(image: UIImage(named: "icon_sz_arrow"))
        temp.addSubview(imgView)
        imgView.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.right.equalTo(-25)
        }
        
        return temp
    }
    
    @objc func itemViewClick(sender: UIGestureRecognizer) {
        guard let temp = sender.view else {
            return
        }
        var url: URL?
        if temp == serviceView {
            url = URL(string: AppConfig.SERVICE_URL)
        }else {
            url = URL(string: AppConfig.PRIVACY_URL)
        }
        if let tempUrl = url {
            present(SFSafariViewController(url: tempUrl), animated: true, completion: nil)
        }
    }


}
